<?php
require_once dirname(__FILE__) . '/../config/config.php';
class AppMessageController
{
    private $smarty;
    public function __construct()
    {
    }

    public function main(&$smarty)
    {
        $this->smarty = $smarty;
    }

    public function listAction()
    {
        $context = new BaseContext();
        $result = new ServiceResult();
        $appMessageService = new AppMessageService();
        $appService = new AppService();
        $pager = new Pager();        
        $curpage = isset ($_GET['page']) ? $_GET['page'] : 1;
        
        $appNameArr = $appService -> getAppNameByStatus($context, $result, 1);
        if(isset($_POST) && !empty($_POST))
	    {
        	$filter = $_POST['filter'];
        	if(isset($_GET) && !empty($_GET) && $_GET['filter'] == 'filter')
		    {
		       	$appMessageArr = $appMessageService -> getAppMessageById($context, $result, $filter);			       	
		   	}
		   	$this->smarty->assign('filter', $filter);
	    }
        else 
        {
       		$count = $appMessageService -> countAppMessage($context, $result);
        	$limit = 30;
        	$start = $pager->findStart($limit);
        	$pages = $pager->findPages($count, $limit);
        	$appMessageArr = $appMessageService->getAllAppMessage($context, $result, $start, $limit);   
       		$lastStr = "controller=appMessage&action=list";
        }
       	
       	if(isset($pager) && isset($pages) && isset($lastStr))
        {
	        $pageFirst = $pager->findFirst($curpage, $pages, $lastStr);
	        $pagePrev = $pager->findPrev($curpage, $pages, $lastStr);
	        $pageList = $pager->pageList($curpage, $pages, $lastStr);
	        $pageNext = $pager->findNext($curpage, $pages, $lastStr);
	        $pageLast = $pager->findLast($curpage, $pages, $lastStr);
	
	        $this->smarty->assign('pageFirst', $pageFirst);
	        $this->smarty->assign('pagePrev', $pagePrev);
	        $this->smarty->assign('pageList', $pageList);
	        $this->smarty->assign('pageNext', $pageNext);
	        $this->smarty->assign('pageLast', $pageLast);
        }
        if(isset($appMessageArr))
        {
        	$this->smarty->assign('appMessageArr', $appMessageArr);
        	$this->smarty->assign('appNameArr', $appNameArr);
        }
        $this->smarty->display('list-app-message.tpl');
    }

    public function addAction()
    {
        $_SESSION['timeout'] = time();
        $context = new BaseContext();
        $result = new ServiceResult();
        $appMessageService = new AppMessageService();
        $appService = new AppService();
        
        $appNameArr = $appService -> getAppNameByStatus($context, $result, 1);
		
        if(isset($_POST) && !empty($_POST))
        {       
            $param = $_POST;
          
            $appMessageService->insertAppMessage($context, $result, $param);
            header("location:".WEB_BASE_URL."/index.php?controller=appMessage&action=list");
        }
        else
        {
            $this->smarty->assign('appNameArr', $appNameArr);
            $this->smarty->display('add-app-message.tpl');
        }
    }

    public function updateAction()
    {
    	$_SESSION['timeout'] = time();
        $context = new BaseContext();
        $result = new ServiceResult();
        $appMessageService = new AppMessageService();
        $appService = new AppService();
    	if(isset($_GET['id']) && !empty($_GET['id']))
        {
           	$id = $_GET['id'];
        }
        $appNameArr = $appService -> getAppNameByStatus($context, $result, 1);
        $appMessage = $appMessageService -> getAppMessageById($context, $result, $id);
		
        if(isset($_POST) && !empty($_POST))
        {       
            $param = $_POST;
          
            $appMessageService->updateAppMessage($context, $result, $param);
            header("location:".WEB_BASE_URL."/index.php?controller=appMessage&action=list");
        }
        else
        {
        	$this->smarty->assign('appMessage', $appMessage);
            $this->smarty->assign('appNameArr', $appNameArr);
            $this->smarty->display('update-app-message.tpl');
        }
    }

    public function deleteAction()
    {
        $context = new BaseContext();
        $result = new ServiceResult();
    	$appMessageService = new AppMessageService();
        if(isset($_GET['id']) && !empty($_GET['id']))
        {
            $id = $_GET['id'];
        }
        $appMessageService->deleteAppMessage($context, $result, $id);
        header("location:".WEB_BASE_URL."/index.php?controller=appMessage&action=list");	
		
    }
}

?>